package comm.android.sherry.servicetest;

import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    //显示组件
    private Button btn_StartService;
    private Button btn_StopService;
    private Button btn_BindService;
    private Button btn_UnbindService;

    //ServiceConnection在Context.bindService()和Context.unBindService()里用到
    private ServiceConnection mServiceConnection;

    private MyService.DownloadBinder downloadBinder;

    @Override
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        setupViews();

        mServiceConnection = new ServiceConnection(){
            @Override
            public void onServiceConnected(ComponentName name, IBinder service){
                downloadBinder = (MyService.DownloadBinder)service;
                downloadBinder.startDownload();
                downloadBinder.getProgress();
            }
            @Override
            public void onServiceDisconnected(ComponentName name){
            }
        };
    }

    private void setupViews(){
        btn_StartService = (Button)findViewById(R.id.button1);
        btn_StopService = (Button)findViewById(R.id.button2);
        btn_BindService = (Button)findViewById(R.id.button3);
        btn_UnbindService = (Button)findViewById(R.id.button4);

        btn_StartService.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View view){
                Intent intent = new Intent(MainActivity.this,MyService.class);
                startService(intent);
            }
        });
        btn_StopService.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View view){
                Intent intent = new Intent(MainActivity.this,MyService.class);
                stopService(intent);
            }
        });
        btn_BindService.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View view){
                Intent intent = new Intent(MainActivity.this,MyService.class);
                bindService(intent,mServiceConnection,BIND_AUTO_CREATE); //绑定服务
            }
        });
        btn_UnbindService.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View view){
                Intent intent = new Intent(MainActivity.this,MyService.class);
                unbindService(mServiceConnection); //解绑服务
            }
        });
    }

    @Override
    protected void onDestroy(){
        Log.e("MainActivity","Activity.onDestroy()");
        Toast.makeText(this,"45Activity.onDestroy()",Toast.LENGTH_SHORT).show();
        super.onDestroy();
    }
}
